﻿using Abp.Dapper.Repositories;
using Abp.Domain.Repositories;
using AbpDemo.Entity;
using study.EmployeeTaskMappings.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study.EmployeeTaskMappings
{
    public class EmployeeTaskMappingAppService:studyAppServiceBase,IEmployeeTaskMappingAppService
    {
        private readonly IRepository<EmployeeTaskMapping> _mappingRepository;
        private readonly IRepository<Employee> _employeeRepository;
        private readonly IRepository<AbpDemo.Entity.Task> _taskRepository;
        public EmployeeTaskMappingAppService(IRepository<EmployeeTaskMapping> mappingRepository,
                                             IRepository<Employee> employeeRepository,
                                             IRepository<AbpDemo.Entity.Task> taskRepository
                                             )
        {
            _mappingRepository = mappingRepository;
            _employeeRepository = employeeRepository;
            _taskRepository = taskRepository;
        }
        /// <summary>
        /// 根据时间范围获取排班数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<ArrangeDto> OutputArrangeWork(DateRangeDto input)
        {
            List<ArrangeDto> arrangeWorkList = new List<ArrangeDto>();
            var outputList = _mappingRepository.GetAllList().Where(c => DateTime.Compare(c.WorkTime, input.StartDate) >= 0 && DateTime.Compare(c.WorkTime, input.EndDate) <= 0);
            foreach (EmployeeTaskMapping employeeTaskMapping in outputList)
            {
                var employeeName = _employeeRepository.Get(employeeTaskMapping.EmployeeId).Name;
                var taskName = _taskRepository.Get(employeeTaskMapping.TaskId).Code;
                var addDate = new ArrangeDto() { EmployeeName = employeeName, TaskName = taskName, WorkTime = employeeTaskMapping.WorkTime };
                arrangeWorkList.Add(addDate);
            }
            return arrangeWorkList;
        }
        /// <summary>
        /// 获取初始化的时间敢为2周
        /// </summary>
        /// <returns></returns>
        public List<string> GetStartTimeAndEndTime()
        {
            List<string> timeList = new List<string>();
            var mon = GetMonday();
            var sun = mon.AddDays(13);
            timeList.Add(mon.ToString("yyyy-MM-dd"));
            timeList.Add(sun.ToString("yyyy-MM-dd"));
            return timeList;
        }

        /// <summary>
        /// 获取当前日期的周一
        /// </summary>
        /// <returns></returns>
        public DateTime GetMonday()
        {
            DateTime dt = DateTime.Now;
            int today = (int)dt.DayOfWeek;//获取今天周几
            if (dt.DayOfWeek.ToString() != "Sunday")//也可以使用today!=0
            { return dt.AddDays((today-1)*-1).Date; }
            else
            { return dt.AddDays(-6 - today).Date; }//若今天是周日，获取到的周一日期是下周一的日期，所以要减去7天
        }

        /// <summary>
        /// 获取所有的员工
        /// </summary>
        /// <returns></returns>
        public List<GetEmployeeDto> GetAllEmployee()
        {
            var allList = _employeeRepository.GetAllList();
            List<GetEmployeeDto> employeeList = new List<GetEmployeeDto>();           
            foreach (Employee e in allList)
            {
                var employee = new GetEmployeeDto() { EmployeeId = e.Id, Name = e.Name };
                employeeList.Add(employee);
            }
            return employeeList;
        }
        /// <summary>
        /// 新建排班
        /// </summary>
        /// <param name="input"></param>
        public void CreateArrangeWork(CreateArrangeDto input)
        {
            DateTime date = Convert.ToDateTime(input.DateStr);
            int employeeId = Convert.ToInt32(input.EmployeeId);
            int taskId = Convert.ToInt32(input.TaskId);
            var insert = new EmployeeTaskMapping() { EmployeeId = employeeId, TaskId = taskId, WorkTime = date };
            _mappingRepository.Insert(insert);
        }
        
        /// <summary>
        /// 编辑更新排班表
        /// </summary>
        /// <param name="input"></param>
        public void UpdateArrangeWork(CreateArrangeDto input)
        {
            DateTime date = Convert.ToDateTime(input.DateStr);
            int employeeId = Convert.ToInt32(input.EmployeeId);
            int taskId = Convert.ToInt32(input.TaskId);
            var arragenWork = _mappingRepository.GetAll().SingleOrDefault(c => c.EmployeeId == employeeId && c.WorkTime == date);
            arragenWork.TaskId = taskId;
            _mappingRepository.Update(arragenWork);
        }
        /// <summary>
        /// 删除排班表的某一个数据
        /// </summary>
        /// <param name="input"></param>
        public void DelArrangeWork(CreateArrangeDto input)
        {
            DateTime date = Convert.ToDateTime(input.DateStr);
            int employeeId = Convert.ToInt32(input.EmployeeId);
            int taskId = Convert.ToInt32(input.TaskId);
            var mappingId = _mappingRepository.GetAll().SingleOrDefault(c => c.EmployeeId == employeeId && c.TaskId == taskId && c.WorkTime == date).Id;
            var ArrangeWork = _mappingRepository.Get(mappingId);
            _mappingRepository.Delete(ArrangeWork);
        }
        
        public void PasteArrangeWork(CopyAndPasteDto input)
        {
            DateTime startCopyDate = Convert.ToDateTime(input.StartCopyDate);
            DateTime endCopyDate = Convert.ToDateTime(input.EndCopyDate);
            DateTime pasteDate = Convert.ToDateTime(input.PasteDate);
            var copyList = _mappingRepository.GetAllList().Where(e => DateTime.Compare(e.WorkTime, startCopyDate) >= 0 && DateTime.Compare(e.WorkTime, endCopyDate) <= 0);
            int addDay = pasteDate.Subtract(startCopyDate).Days;          
            
            foreach (EmployeeTaskMapping item in copyList)
            {
                DateTime addTime = item.WorkTime.AddDays(addDay);
                var isCreate = _mappingRepository.GetAll().SingleOrDefault(m => m.WorkTime == addTime && m.EmployeeId == item.EmployeeId);
                if (isCreate != null)
                {
                    isCreate.TaskId = item.TaskId;
                    _mappingRepository.Update(isCreate);
                }
                else
                {
                    var insert = new EmployeeTaskMapping() { EmployeeId = item.EmployeeId, TaskId = item.TaskId, WorkTime = addTime };

                    _mappingRepository.Insert(insert);
                }
            }
            

        }


    }
}
